﻿package cn.antscript.model 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	/**
	 * ...
	 * @author 
	 */
	public class AutoURLRequest extends EventDispatcher
	{
		private static var _instance:AutoURLRequest;
		
		public var xmlData:XML;
		
		public function AutoURLRequest(privateClass:PrivateClass) 
		{
			
		}
		
		public static  function getInstance():AutoURLRequest{
			if (_instance == null) {
				_instance = new AutoURLRequest(new PrivateClass);
			}
			return _instance;
		}
		
		public function load(url:String,vars:URLVariables=null):void {
			var loader:URLLoader = new URLLoader();
			var req:URLRequest = new URLRequest(url);
			if (vars != null) {
				req.data = vars;
			}
			loader.load(req);
			loader.addEventListener(Event.COMPLETE, onCom);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onErr);
		}
		
		private function onCom(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			xmlData = XML(e.target.data);
			
			dispatchEvent(e);
		}
		
		private function onErr(e:IOErrorEvent):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onErr);			
			
			dispatchEvent(e);
		}
		
		
	}

}

class PrivateClass {
	
}